package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.StudifMapper;
import com.example.mybatisplus.model.domain.Tea;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.StudifService;
import com.example.mybatisplus.model.domain.Studif;

import java.util.ArrayList;
import java.util.List;


/**
 * 前端控制器
 *
 * @author yp
 * @version v1.0
 * @since 2021-10-26
 */
@Controller
@RequestMapping("/api/studif")
public class StudifController {

    private final Logger logger = LoggerFactory.getLogger(StudifController.class);

    @Autowired
    private StudifService studifService;
    @Autowired
    private StudifMapper studifMapper;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        Studif studif = studifService.getById(id);
        return JsonResponse.success(studif);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        studifService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateStudif(Studif studif) throws Exception {
        studifService.updateById(studif);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建Studif
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Studif studif) throws Exception {
        studifService.save(studif);
        return JsonResponse.success(null);
    }

    /**
     * 根据登录账号查看当前批次是否已经申请
     */
    @PostMapping("/add")
    @ResponseBody
    public JsonResponse add(@RequestBody Studif studif) {
        Studif studif1 = studifMapper.selectByLoginIDWithYear(SessionUtils.getCurrentUserInfo().getId(), studif.getStuYear());
        return JsonResponse.success(studif1);
    }

    /**
     * 查询当前用户申请表信息
     */
    @GetMapping("/thing")
    @ResponseBody
    public JsonResponse thing() {
        List<Studif> list = new ArrayList<>();
        list.add(studifMapper.selectByLoginId(SessionUtils.getCurrentUserInfo().getId()));
        return JsonResponse.success(list);
    }

    /**
     * 学生用户申请表的插入
     */
    @PostMapping("/insertStu")
    @ResponseBody
    public JsonResponse insertStu(@RequestBody Studif studif) {
        studif.setAdId(SessionUtils.getCurrentUserInfo().getId());
        studifMapper.insert(studif);
        return JsonResponse.success(null);
    }

    /**
     * 学生用户修改申请表
     */
    @PostMapping("/updatastu")
    @ResponseBody
    public JsonResponse updateStu(@RequestBody Studif studif) {
        studifMapper.updateByStuNumber(studif.getStuNumber(), studif);
        return JsonResponse.success(null);
    }

    /**
     * 学生删除申请表
     */
    @PostMapping("/deleteStu")
    @ResponseBody
    public JsonResponse deleteStu(@RequestBody Studif studif) {
        studifMapper.deleteByStuNumber(studif.getStuNumber(), studif.getStuYear());
        return JsonResponse.success(null);
    }

    /**
     * 学生申请表提交
     */
    @PostMapping("/toggleInsert")
    @ResponseBody
    public JsonResponse toggleInsert(@RequestBody Studif studif) {
        studifMapper.insertWithStudif(studif.getStuNumber(), studif.getStuYear());
        return JsonResponse.success(null);
    }

    /**
     * 学生进行操作前应该先进行判断是否已经提交
     */
    @PostMapping("/selecetInsert")
    @ResponseBody
    public JsonResponse selecetInsert(@RequestBody Studif studif) {
        Studif i = studifMapper.selectInsert(studif.getStuNumber(), studif.getStuYear());
        return JsonResponse.success(i);
    }

    /**
     * 教师端获取当前申请批次的信息
     */

    @GetMapping("/getstuList")
    @ResponseBody
    public JsonResponse getstuList() {
        List<Studif> list = studifService.list();
        return JsonResponse.success(list);

    }

    /**
     * 教师端分页查询学生信息
     */
    @GetMapping("/getpageStu")
    @ResponseBody
    public JsonResponse getpageStu(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Studif> page = new Page<>(pageNo, pageSize);
        page = studifService.page(page);
        return JsonResponse.success(page);
    }

    /**
     * 学院端分页查询学生信息
     */
    @GetMapping("/scgetpageStu")
    @ResponseBody
    public JsonResponse scgetpageStu(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Studif> page = new Page<>(pageNo, pageSize);
        page = studifService.page(page);
        return JsonResponse.success(page);
    }

    /**
     * 教师提交审核状态
     */
    @PostMapping("/confirm1")
    @ResponseBody
    public JsonResponse confirm1(@RequestBody Studif studif) {
        studifMapper.insertWithTea(studif.getStuNumber(), studif.getStuYear(), studif.getStuTeacherth(), studif.getStuTeachers());
        return JsonResponse.success(null);
    }

    /**
     * 学院端提交审核状态
     */
    @PostMapping("/confirm2")
    @ResponseBody
    public JsonResponse confirm2(@RequestBody Studif studif) {
        studifMapper.insertWithSchool(studif.getStuNumber(), studif.getStuYear(), studif.getStuSchoolth(), studif.getStuSchools(),studif.getStuDif());
        return JsonResponse.success(null);
    }

    /**
     * 学生查询审核状态
     */
    @PostMapping("/selecetInstudif")
    @ResponseBody
    public JsonResponse selecetInstudif(@RequestBody Studif studif) {
        Studif studif1 = studifMapper.selectByShenhe(studif);
        return JsonResponse.success(studif1);
    }

    /**
     * 学院端模糊搜索查询
     */
    @PostMapping("/selectionWithStudent")
    @ResponseBody
    public JsonResponse selectionWithStudent(@RequestBody Studif studif) {
        List<Studif> studifs = studifService.selectBySchool(studif);
        return JsonResponse.success(studifs);
    }
}

